package ventanas;

import isi_graf.isiBenChart;
import isi_graf.isi_DualFinanceChart;
import isi_ib.MyContract;
import isi_ib.barra_trade;
import isi_ib.barras_trade;
import isi_ib.historico_ib;
import isi_ib.isi_estudios;
import isi_ib.isi_arrays;
import isi_ib.reltime_bars_ib;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import javax.swing.JComboBox;

import org.jfree.chart.ChartPanel;

import utiles_isi.dateutil;
import utiles_isi.isi_conexion;
import utiles_isi.isi_util;

import javax.swing.JList;
import javax.swing.JSpinner;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JCheckBox;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JSeparator;
import java.awt.Font;

public class Config_graf extends JFrame {

	private JPanel contentPane;
	JComboBox comboBox;
	JButton btnCargaDatos;
	JList list ;
	//private isi_arrays im;
	isi_DualFinanceChart dualtimechart_pasos;
	private barras_trade barras_pasos;
	private barra_trade barra_pasos;
	private isi_estudios estudios_pasos; 
	
	ArrayList<isi_DualFinanceChart> graficos;
	
	
	private historico_ib historico;
	private MyContract ccontrato;
	private Alerta vAlerta;
	private reltime_bars_ib realbars;
	
	
	private JButton btnGraficoDa;
	private JTextField textField1;
	private JSpinner spinner;
	private JCheckBox cbMedias;
	private JCheckBox cbOpenClose;
	private JCheckBox cbVolatilidad;
	private JCheckBox cbsignal;
	private JCheckBox cbdifoscilador;
	private JCheckBox cbdifmedias;
	private JCheckBox cbOsciladores;
	private JCheckBox chckbxMacdh;
	private JTextField textFieldcoste;
	private JTextField textsocparam;
	private JTextField textFielddifosc;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_5;
	private JTextArea textArea;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField Limite_entrada;
	private JTextField Limite_salida;
	private JTextField Limite_entrada_tendencia_1;
	private JTextField Limite_entrada_tendencia_2;
	private JTextField Limite_perdidas;
	private JTextField Limite_movil_beneficios;
	private JTextField Triger_beneficios;
	private JTextField texMedia_R;
	private JTextField texMedia_I;
	private JTextField texMedia_L;
	private JSeparator separator_1;
	private JLabel lblMacd;
	private JTextField Limite_entrada_tendencia_MED_1;
	private JTextField Limite_entrada_tendencia_MED_2;
	private JTextField MaximaDistanciaEntrada;
	private JTextField ModuloDesviacion;
	private JTextField BaseDesviacion;
	private JTextField tPeriodo;
	private JTextField tIntervalo;
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Config_graf frame = new Config_graf(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public Config_graf(historico_ib historico_,MyContract ccontrato_,reltime_bars_ib realbars) {
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		dualtimechart_pasos=null;
		barras_pasos=null;
		barra_pasos=null;
		estudios_pasos=null;
		if(historico_==null) {historico=new historico_ib();	}
		else historico= historico_;
		
		if(ccontrato_==null){ccontrato=new MyContract("","ES");	}
		else ccontrato=ccontrato_;
		
		historico_.setContract(ccontrato.contrato);
		vAlerta=null;
		graficos = new ArrayList<isi_DualFinanceChart>();
		
		//im=new isi_arrays("a",4);
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
		setBounds(100, 100, 825, 567);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		comboBox = new JComboBox();
		
		
		
		
		btnCargaDatos = new JButton("...");
		btnCargaDatos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {cargaCombo();	}
		});
		btnCargaDatos.setBounds(0, 0, 63, 23);
		contentPane.add(btnCargaDatos);
		
		
		comboBox.setBounds(64, 1, 134, 20);
		contentPane.add(comboBox);
		
		btnGraficoDa = new JButton("Grafico 1 d\u00EDa");
		btnGraficoDa.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BotonInicia();
			}
		});
		btnGraficoDa.setBounds(0, 34, 89, 23);
		contentPane.add(btnGraficoDa);
		
		list = new JList();
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				textField1.setText(list.getSelectedValue().toString());
				CambiaLimites(charSelected(textField1.getText()));
			}
		});
		list.setBounds(10, 68, 175, 187);
		contentPane.add(list);
		
		JButton btnSelecciona = new JButton("selecciona");
		btnSelecciona.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pintaGrafico();
			}
		});
		btnSelecciona.setBounds(195, 65, 89, 23);
		contentPane.add(btnSelecciona);
		
		spinner = new JSpinner();
		spinner.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				CambiaLimites();
			}
		});
		
		spinner.setModel(new SpinnerNumberModel(new Integer(3), new Integer(0), null, new Integer(1)));
		
		spinner.setBounds(291, 66, 29, 20);
		contentPane.add(spinner);
		
		textField1 = new JTextField();
		textField1.setBounds(318, 66, 86, 20);
		contentPane.add(textField1);
		textField1.setColumns(10);
		
		cbMedias = new JCheckBox("Medias");
		cbMedias.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbMedias.setBounds(191, 95, 59, 23);
		contentPane.add(cbMedias);
		
		cbOpenClose = new JCheckBox("Open/Close");
		cbOpenClose.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbOpenClose.setBounds(259, 95, 97, 23);
		contentPane.add(cbOpenClose);
		
		cbVolatilidad = new JCheckBox("Volatilidad");
		cbVolatilidad.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbVolatilidad.setBounds(191, 121, 73, 23);
		contentPane.add(cbVolatilidad);
		
		cbsignal = new JCheckBox("Se\u00F1al");
		cbsignal.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbsignal.setBounds(269, 121, 97, 23);
		contentPane.add(cbsignal);
		
		cbdifoscilador = new JCheckBox("Dif Oscilador");
		cbdifoscilador.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbdifoscilador.setBounds(191, 174, 86, 23);
		contentPane.add(cbdifoscilador);
		
		cbdifmedias = new JCheckBox("MACD");
		cbdifmedias.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbdifmedias.setBounds(191, 147, 63, 23);
		contentPane.add(cbdifmedias);
		
		JButton btnNewButton = new JButton("Guarda properties");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			//	im.add(3, 5);
				GuardaEstudioProperties();
			}
		});
		btnNewButton.setBounds(96, 34, 182, 23);
		contentPane.add(btnNewButton);
		
		cbOsciladores = new JCheckBox("Osciladores : M,SK,SD");
		cbOsciladores.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
		cbOsciladores.setBounds(195, 208, 142, 23);
		contentPane.add(cbOsciladores);
		
		textFieldcoste = new JTextField();
		textFieldcoste.setBounds(414, 66, 86, 20);
		contentPane.add(textFieldcoste);
		textFieldcoste.setColumns(10);
		
		JButton btnCalculaBeneficios = new JButton("Calcula beneficios");
		btnCalculaBeneficios.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cargaComboBeneficios();
			}
		});
		btnCalculaBeneficios.setBounds(195, 286, 182, 23);
		contentPane.add(btnCalculaBeneficios);
		
		textsocparam = new JTextField();
		textsocparam.setText("14,7,2");
		textsocparam.setBounds(343, 209, 45, 20);
		contentPane.add(textsocparam);
		textsocparam.setColumns(10);
		
		JButton btnNewButton_1 = new JButton(">>");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CalculaBeneficios();
			}
		});
		btnNewButton_1.setBounds(291, 319, 49, 23);
		contentPane.add(btnNewButton_1);
		
		textFielddifosc = new JTextField();
		textFielddifosc.setText("2,4,1");
		textFielddifosc.setBounds(291, 173, 63, 23);
		contentPane.add(textFielddifosc);
		textFielddifosc.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(195, 320, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		chckbxMacdh = new JCheckBox("MACDH");
		chckbxMacdh.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				CambiaLimites();
			}
		});
			
		chckbxMacdh.setBounds(259, 147, 63, 23);
		contentPane.add(chckbxMacdh);
		
		JButton btnNuevoGrafPasos = new JButton("Nuevo Graf pasos");
		btnNuevoGrafPasos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				IniciaGraficoPaso();
			}
		});
		btnNuevoGrafPasos.setBounds(195, 363, 182, 23);
		contentPane.add(btnNuevoGrafPasos);
		
		JButton btnSiguiente = new JButton("Siguiente");
		btnSiguiente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SiguientePaso();
			}
		});
		btnSiguiente.setBounds(195, 397, 89, 23);
		contentPane.add(btnSiguiente);
		
		textField_3 = new JTextField();
		textField_3.setBounds(291, 398, 86, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(387, 398, 86, 20);
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		
		textArea = new JTextArea();
		textArea.setBounds(0, 274, 185, 229);
		contentPane.add(textArea);
		
		textField_6 = new JTextField();
		textField_6.setBounds(291, 429, 86, 20);
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		
		textField_7 = new JTextField();
		textField_7.setBounds(387, 429, 86, 20);
		contentPane.add(textField_7);
		textField_7.setColumns(10);
		
		JButton btnNewButton_2 = new JButton("Cargar prperties");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CargaEstudioProperties();
			}
		});
		btnNewButton_2.setBounds(291, 34, 182, 23);
		contentPane.add(btnNewButton_2);
		
		JPanel panel = new JPanel();
		panel.setBounds(543, 21, 264, 501);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Entrada");
		lblNewLabel.setBounds(0, 3, 54, 14);
		panel.add(lblNewLabel);
		
		Limite_entrada = new JTextField();
		Limite_entrada.setBounds(64, 0, 65, 20);
		panel.add(Limite_entrada);
		Limite_entrada.setColumns(10);
		
		JLabel lblCruce = new JLabel("Salida");
		lblCruce.setBounds(139, 3, 50, 14);
		panel.add(lblCruce);
		
		Limite_salida = new JTextField();
		Limite_salida.setBounds(194, 0, 70, 20);
		Limite_salida.setColumns(10);
		panel.add(Limite_salida);
		
		JLabel lblTrend = new JLabel("Trend 1");
		lblTrend.setBounds(0, 82, 54, 14);
		panel.add(lblTrend);
		
		Limite_entrada_tendencia_1 = new JTextField();
		Limite_entrada_tendencia_1.setBounds(64, 79, 65, 20);
		Limite_entrada_tendencia_1.setColumns(10);
		panel.add(Limite_entrada_tendencia_1);
		
		JLabel lblTrend_1 = new JLabel("Trend 2");
		lblTrend_1.setBounds(152, 82, 37, 14);
		panel.add(lblTrend_1);
		
		Limite_entrada_tendencia_2 = new JTextField();
		Limite_entrada_tendencia_2.setBounds(194, 79, 70, 20);
		Limite_entrada_tendencia_2.setColumns(10);
		panel.add(Limite_entrada_tendencia_2);
		
		JLabel lblLmPerdidas = new JLabel("L\u00EDm perdidas");
		lblLmPerdidas.setBounds(0, 127, 59, 14);
		panel.add(lblLmPerdidas);
		
		Limite_perdidas = new JTextField();
		Limite_perdidas.setBounds(64, 124, 65, 20);
		Limite_perdidas.setColumns(10);
		panel.add(Limite_perdidas);
		
		JLabel lblLmMovBen = new JLabel("L\u00EDm mov Ben");
		lblLmMovBen.setBounds(0, 152, 59, 14);
		panel.add(lblLmMovBen);
		
		Limite_movil_beneficios = new JTextField();
		Limite_movil_beneficios.setBounds(64, 149, 65, 20);
		Limite_movil_beneficios.setColumns(10);
		panel.add(Limite_movil_beneficios);
		
		JLabel lblTriggerBen = new JLabel("Trigger Ben");
		lblTriggerBen.setBounds(134, 152, 55, 14);
		panel.add(lblTriggerBen);
		
		Triger_beneficios = new JTextField();
		Triger_beneficios.setBounds(194, 149, 70, 20);
		Triger_beneficios.setColumns(10);
		panel.add(Triger_beneficios);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(0, 180, 264, 2);
		panel.add(separator);
		
		JLabel lblMedias = new JLabel("Medias");
		lblMedias.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblMedias.setBounds(0, 183, 46, 14);
		panel.add(lblMedias);
		
		JLabel lblRI = new JLabel("R - I - L");
		lblRI.setBounds(0, 206, 54, 14);
		panel.add(lblRI);
		
		texMedia_R = new JTextField();
		texMedia_R.setColumns(10);
		texMedia_R.setBounds(64, 203, 46, 20);
		panel.add(texMedia_R);
		
		texMedia_I = new JTextField();
		texMedia_I.setColumns(10);
		texMedia_I.setBounds(120, 203, 46, 20);
		panel.add(texMedia_I);
		
		texMedia_L = new JTextField();
		texMedia_L.setColumns(10);
		texMedia_L.setBounds(176, 203, 46, 20);
		panel.add(texMedia_L);
		
		separator_1 = new JSeparator();
		separator_1.setBounds(0, 231, 264, 2);
		panel.add(separator_1);
		
		lblMacd = new JLabel(" MACD - M\u00F3dulo r\u00E1pido,lento,se\u00F1al,se\u00F1al1");
		lblMacd.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblMacd.setBounds(0, 237, 254, 14);
		panel.add(lblMacd);
		
		textField_1 = new JTextField();
		textField_1.setBounds(0, 255, 78, 20);
		panel.add(textField_1);
		textField_1.setText("20,4,9,3");
		textField_1.setColumns(10);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(0, 106, 264, 2);
		panel.add(separator_2);
		
		JLabel lblControlPerdidas = new JLabel("Control perdidas");
		lblControlPerdidas.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblControlPerdidas.setBounds(0, 109, 129, 14);
		panel.add(lblControlPerdidas);
		
		JLabel lblLimitesTendenciaMac = new JLabel("Limites tendencia MAC1");
		lblLimitesTendenciaMac.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblLimitesTendenciaMac.setBounds(0, 65, 156, 14);
		panel.add(lblLimitesTendenciaMac);
		
		JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(0, 65, 264, 2);
		panel.add(separator_3);
		
		JLabel lblLimitesTendenciaMedia = new JLabel("Limites tendencia MEDIA");
		lblLimitesTendenciaMedia.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblLimitesTendenciaMedia.setBounds(0, 20, 166, 14);
		panel.add(lblLimitesTendenciaMedia);
		
		JLabel label_1 = new JLabel("Trend 1");
		label_1.setBounds(0, 44, 54, 14);
		panel.add(label_1);
		
		Limite_entrada_tendencia_MED_1 = new JTextField();
		Limite_entrada_tendencia_MED_1.setColumns(10);
		Limite_entrada_tendencia_MED_1.setBounds(64, 41, 65, 20);
		panel.add(Limite_entrada_tendencia_MED_1);
		
		JLabel label_2 = new JLabel("Trend 2");
		label_2.setBounds(152, 44, 37, 14);
		panel.add(label_2);
		
		Limite_entrada_tendencia_MED_2 = new JTextField();
		Limite_entrada_tendencia_MED_2.setColumns(10);
		Limite_entrada_tendencia_MED_2.setBounds(194, 41, 70, 20);
		panel.add(Limite_entrada_tendencia_MED_2);
		
		MaximaDistanciaEntrada = new JTextField();
		MaximaDistanciaEntrada.setColumns(10);
		MaximaDistanciaEntrada.setBounds(0, 300, 78, 20);
		panel.add(MaximaDistanciaEntrada);
		
		JLabel lblMximaDistanciaA = new JLabel("M\u00E1xima distancia a entrada");
		lblMximaDistanciaA.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblMximaDistanciaA.setBounds(0, 282, 254, 14);
		panel.add(lblMximaDistanciaA);
		
		JSeparator separator_4 = new JSeparator();
		separator_4.setBounds(0, 276, 264, 2);
		panel.add(separator_4);
		
		ModuloDesviacion = new JTextField();
		ModuloDesviacion.setColumns(10);
		ModuloDesviacion.setBounds(51, 345, 78, 20);
		panel.add(ModuloDesviacion);
		
		JLabel lblDesviaciones = new JLabel("Desviaciones");
		lblDesviaciones.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblDesviaciones.setBounds(0, 328, 254, 14);
		panel.add(lblDesviaciones);
		
		JSeparator separator_5 = new JSeparator();
		separator_5.setBounds(0, 322, 264, 2);
		panel.add(separator_5);
		
		JLabel lblMdulo = new JLabel("M\u00F3dulo");
		lblMdulo.setBounds(0, 348, 50, 14);
		panel.add(lblMdulo);
		
		JLabel lblBase = new JLabel("Base");
		lblBase.setBounds(134, 348, 50, 14);
		panel.add(lblBase);
		
		BaseDesviacion = new JTextField();
		BaseDesviacion.setColumns(10);
		BaseDesviacion.setBounds(185, 345, 78, 20);
		panel.add(BaseDesviacion);
		
		JLabel lblFrecuencia = new JLabel("Frecuencia");
		lblFrecuencia.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblFrecuencia.setBounds(0, 373, 254, 14);
		panel.add(lblFrecuencia);
		
		JSeparator separator_6 = new JSeparator();
		separator_6.setBounds(0, 367, 264, 2);
		panel.add(separator_6);
		
		JLabel lblPeriodo = new JLabel("Periodo");
		lblPeriodo.setBounds(0, 393, 50, 14);
		panel.add(lblPeriodo);
		
		tPeriodo = new JTextField();
		tPeriodo.setColumns(10);
		tPeriodo.setBounds(51, 390, 78, 20);
		panel.add(tPeriodo);
		
		JLabel lblIntervalo = new JLabel("Intervalo");
		lblIntervalo.setBounds(134, 393, 50, 14);
		panel.add(lblIntervalo);
		
		tIntervalo = new JTextField();
		tIntervalo.setColumns(10);
		tIntervalo.setBounds(185, 390, 78, 20);
		panel.add(tIntervalo);
		
	/*	JList list_1 = new JList();
		list_1.setBounds(10, 298, 1, 1);
		contentPane.add(list_1);*/
	}
    
	private void log(String txt_){
		if(vAlerta!=null) vAlerta.anadeMensaje("config_graf: "+txt_);
	}
	public void setAlerta(Alerta valerta_){
		vAlerta=valerta_;
		
	}
	

	
private String estudiosProperties(){
	return  Limite_entrada.getText()+","+
			Limite_salida.getText()+","+
			Limite_entrada_tendencia_1.getText()+","+
			Limite_entrada_tendencia_2.getText()+","+
			Limite_perdidas.getText()+","+
			Limite_movil_beneficios.getText()+","+
			Triger_beneficios.getText()+","+
			Limite_entrada_tendencia_MED_1.getText()+","+
			Limite_entrada_tendencia_MED_2.getText()+","+
			MaximaDistanciaEntrada.getText();
			
			
}
private void CargaEstudioProperties(){
		
		isi_estudios estu1 =new isi_estudios(null,null,"",false);
		estu1.Carga();
		textField_1.setText(estu1.String_MAC());
		/*****************************************************************************/
		Limite_entrada.setText(String.valueOf(estu1.limite_entrada));
		Limite_salida.setText(String.valueOf(estu1.limite_salida));
		Limite_entrada_tendencia_1.setText(String.valueOf(estu1.limite_entradatndencia_1));
		Limite_entrada_tendencia_2.setText(String.valueOf(estu1.limite_entradatndencia_2));
		
		Limite_entrada_tendencia_MED_1.setText(String.valueOf(estu1.limite_entradatndencia_med_1));
		Limite_entrada_tendencia_MED_2.setText(String.valueOf(estu1.limite_entradatndencia_med_2));
		
		MaximaDistanciaEntrada.setText(String.valueOf(estu1.max_distancia_entrada));;
				
		Limite_perdidas.setText(String.valueOf(estu1.limite_perdidas));
		Limite_movil_beneficios.setText(String.valueOf(estu1.limite_movil_beneficios));
		Triger_beneficios.setText(String.valueOf(estu1.triger_beneficios));
		/*****************************************************************************/
		/*****************************************************************************/
		texMedia_R.setText(String.valueOf(estu1.media_rapida));
		texMedia_I.setText(String.valueOf(estu1.media_intermedia));
		texMedia_L.setText(String.valueOf(estu1.media_lenta));
		ModuloDesviacion.setText(String.valueOf(estu1.modulo_desviacion));
		BaseDesviacion.setText(String.valueOf(estu1.base_desviacion));
		
		tPeriodo.setText(isi_util.CampoTexto(estu1.StringAcumulado(), 0,","));
		//tIntervalo.setText(isi_util.CampoTexto(estu1.StringAcumulado(), 1,","));
		
		
		
		/*****************************************************************************/
		
		
		
	}
	
	private void GuardaEstudioProperties(){
		
		isi_estudios estu1 =new isi_estudios(null,null,"",false);
		estu1.IniciaMAC(textField_1.getText());
		
		estu1.Inicia_signal(Limite_entrada.getText(),
						  Limite_salida.getText(),
				  Limite_entrada_tendencia_1.getText(),
				  Limite_entrada_tendencia_2.getText() ,
				  Limite_perdidas.getText(),
				  Limite_movil_beneficios.getText(),
				  Triger_beneficios.getText(),
				  Limite_entrada_tendencia_MED_1.getText(),
				  Limite_entrada_tendencia_MED_2.getText(),
				  MaximaDistanciaEntrada.getText()
				  			
				
				);
		estu1.IniciaMedias (texMedia_R.getText(),texMedia_I.getText(),texMedia_L.getText(),ModuloDesviacion.getText(),BaseDesviacion.getText());
		estu1.IniciaAcumulado(tPeriodo.getText());
		estu1.Guarda();
	}
	
	private void cargaCombo(){
		
		barras_trade barras_ = new barras_trade();
		Iterator<String> itr = barras_.ListafechasSQL().iterator(); 
		int i=0;comboBox.removeAllItems();
		while (itr.hasNext()) {
			String element=itr.next();
			comboBox.insertItemAt(element,i);
			i++;
		}
		
		
	}
	
		
	
	private void SiguientePaso()
	{
		int indice_;
		int estado_=0;
		double valor_t;
		int comprar_=0;
		barra_trade comp_;
		String calcula_;
		
		log("Count:"+String.valueOf(barras_pasos.count()));
		//log(" barra ant ---------------");
		//log(barra_pasos.toString());
		
		if(barras_pasos!=null && dualtimechart_pasos!=null){
			
			
			
			indice_=barras_pasos.barras.indexOf(barra_pasos)+1;
			if(indice_<barras_pasos.count()){
				 
				
				log("Indice:"+String.valueOf(indice_));
				log(" barra now ---------------");
				
				barra_pasos=barras_pasos.Get(indice_);
				
				log(barra_pasos.toString());
				log(" ---------------");
				
				//dualtimechart_pasos.Add(barra_pasos);
				
				estudios_pasos.Add(barra_pasos);
				
				log(estudios_pasos.barras.LastAdd().toString());
				log("Count Est:"+String.valueOf(estudios_pasos.barras.count()));
				log(" ---------------");
				log(dualtimechart_pasos.estudios.barras.LastAdd().toString());
				log("Count Chart:"+String.valueOf(dualtimechart_pasos.estudios.barras.count()));
				log(" ---------------");
				
				
				//estado_=estudios_pasos.estado_compra_venta(indice_);
				//comprar_=estudios_pasos.compra_vende(indice_);
				//log("Estado:"+String.valueOf(estado_)+" Comprar:"+String.valueOf(comprar_));
				
			}
			textField_3.setText(
					String.valueOf(dualtimechart_pasos.estudios.Count())
					+"- ("+String.valueOf(estado_)+" )"							
					);
			if(comprar_!=0)
			{
				valor_t=Double.parseDouble(textField_5.getText());
				valor_t=valor_t-comprar_*barra_pasos.getClose();
				textField_5.setText(String.valueOf(valor_t));
				textArea.append(barra_pasos.getDateString()+" "+String.valueOf(comprar_)+"-->"+String.valueOf(barra_pasos.getClose()+"\r\n"));
			}
			
			if(estado_==0)textField_6.setText(	String.valueOf(Double.parseDouble(textField_5.getText())*50) );
			
			//dualtimechart_pasos.rellenaTodo();
			calcula_ = String.valueOf(estudios_pasos.CalculaBeneficios(true,false))+"-"+String.valueOf(estudios_pasos.CalculaBeneficios(true,true));
			textField_7.setText(calcula_);
			CambiaLimites(dualtimechart_pasos);
		
			//dualtimechart_pasos.rellenaTodo();
		}
		
		log(" Sale ---------------");
		
		
	}
		
	private void IniciaGraficoPaso(){
		 
		barras_pasos= new barras_trade();
		barra_pasos=new barra_trade();
		barras_pasos.cargaSql(comboBox.getSelectedItem().toString(),true);
		barra_pasos=barras_pasos.Get(0);
		estudios_pasos = new isi_estudios(null,null,"",false);
		RellenaEstudios(estudios_pasos,true);
		textField_5.setText("0");
		dualtimechart_pasos= new isi_DualFinanceChart("F - "+comboBox.getSelectedItem().toString(),"values",0);
		ChartPanel panel2=dualtimechart_pasos.CreaChart();
		dualtimechart_pasos.Inicia(estudios_pasos,null,dateutil.dateFromString("",comboBox.getSelectedItem().toString()),0);
		Grafico frame2 = new Grafico(panel2);
		frame2.setVisible(true);
		CambiaLimites(dualtimechart_pasos);
			
			
		
				
		
	}
	
	
	
	private void BotonInicia(){
		//barras_paso_a_paso=null;
		
		int max_=MaxIndexcharSelected(comboBox.getSelectedItem().toString())+1;
		
		
			barras_trade barras_ = new barras_trade();
			barras_.setPeriodo(tPeriodo.getText());
			
			
			if(comboBox.getSelectedIndex()>-1){
				//barras_.cargaSql(dateutil.AddDays("",comboBox.getSelectedItem().toString(),-1));
				barras_.cargaSql(comboBox.getSelectedItem().toString(),true); // false
				barras_.creaSql("barras_real_intervalo");
				}
			isi_DualFinanceChart dualtimechart= new isi_DualFinanceChart(comboBox.getSelectedItem().toString(),"values",max_);
			ChartPanel panel2=dualtimechart.CreaChart();
/// debe cargar los datos desde estudios para que se rrelene la media.			
			
			dualtimechart.Inicia(null,barras_,dateutil.dateFromString("",comboBox.getSelectedItem().toString()),0);
			Grafico frame2 = new Grafico(panel2);
			frame2.setVisible(true);
			
			CambiaLimites(dualtimechart);
			
			graficos.add(dualtimechart);
			
			rellenaListaGraficos();	
		
		
		
		
	}
	
	private void RellenaEstudios(isi_estudios estu_, boolean reset_)
	{
		if(reset_) estu_.IniciaAcumulado(this.tPeriodo.getText());
		estu_.IniciaMAC(textField_1.getText());
		estu_.Inicia_signal(estudiosProperties());
		estu_.IniciaMedias (texMedia_R.getText(),texMedia_I.getText(),texMedia_L.getText(),ModuloDesviacion.getText(),BaseDesviacion.getText());
		estu_.IniciaOscilador(textsocparam.getText());
		estu_.IniciaKDDIFF(textFielddifosc.getText()); 
	}
	
	
	private void pintaGrafico(){
		
		isi_DualFinanceChart dualtimechart=charSelected(textField1.getText());
		if(dualtimechart!=null)
		{
			ChartPanel panel2=dualtimechart.getChartPanel();
			Grafico frame2 = new Grafico(panel2);
			frame2.setVisible(true);
			dualtimechart.setLimite(Integer.parseInt(spinner.getValue().toString()));
			
			dualtimechart.IniciaOscilador(textsocparam.getText(), 1);
			dualtimechart.IniciadDifOsciladores(textFielddifosc.getText());
			dualtimechart.Clear();
			dualtimechart.rellenaTodo();
		}
		
	}
	
	private void rellenaListaGraficos(){
		
		
		Iterator<isi_DualFinanceChart> itr = graficos.iterator();
	    int i_=0;
	    DefaultListModel model = new DefaultListModel();
		while (itr.hasNext()) {
	    	isi_DualFinanceChart element = itr.next();
	    	model.add(model.getSize(), element.titulo+"_"+element.CIndice());
	    }
		list.setModel(model);
		}
	
	private isi_DualFinanceChart charSelected(String titulo_,int indice_){
	Iterator<isi_DualFinanceChart> itr = graficos.iterator();
	     while (itr.hasNext()) {
	    	isi_DualFinanceChart element = itr.next();
	    	if(element.titulo.equals(titulo_)){
	    		if(element.Indice()==indice_) return element;
	    	}
	   }
	     
	  
	return null;
	}
	private isi_DualFinanceChart charSelected(String titulo_){
		int indice_=isi_util.isiParseInt(isi_util.CampoTexto(titulo_, 1,"_"));
		titulo_=isi_util.CampoTexto(titulo_,0,"_");
		
		
		return charSelected( titulo_, indice_);
		}
	
	private int MaxIndexcharSelected(String titulo_){
		Iterator<isi_DualFinanceChart> itr = graficos.iterator();
		int max_=-1;
		     while (itr.hasNext()) {
		    	isi_DualFinanceChart element = itr.next();
		    	if(element.titulo.equals(titulo_)){
		    		if(max_<element.Indice()) max_=element.Indice();
		    	}
		   }
		return max_;
		}
	
	
	
	
	

	
	
	private void CambiaLimites(isi_DualFinanceChart ch_){
							
			RellenaEstudios(ch_.estudios,false);
			
			
			
			
			if(ch_!=null){
			
				ch_.Visibilidad(
						cbMedias.isSelected(),cbMedias.isSelected(),cbMedias.isSelected(),
						false,cbOpenClose.isSelected(),
						cbVolatilidad.isSelected(),cbsignal.isSelected(),
						cbdifmedias.isSelected(),chckbxMacdh.isSelected(),
						cbOsciladores.isSelected(),cbOsciladores.isSelected(),cbdifoscilador.isSelected());
				
	/*			ch_.Clear();
			
			ch_.visibleSerie("MEDIA4",cbMedias.isSelected());
			ch_.visibleSerie("MEDIA18",cbMedias.isSelected());
			ch_.visibleSerie("MEDIA40",cbMedias.isSelected());
			ch_.visibleSerie("Open",false);
			ch_.visibleSerie("Close",cbOpenClose.isSelected());
			ch_.visibleSerie("VO",cbVolatilidad.isSelected());
			ch_.visibleSerie("SIGNAL",cbsignal.isSelected());
			//ch_.visibleSerie("Suma",cbdifoscilador.isSelected());
			ch_.visibleSerie("MACD",cbdifmedias.isSelected());
			ch_.visibleSerie("MACDH",chckbxMacdh.isSelected());
			ch_.visibleSerie("OSCILADORK",cbOsciladores.isSelected());
			ch_.visibleSerie("OSCILADORD",cbOsciladores.isSelected());
			ch_.visibleSerie("DIF_OSCILADOR",cbdifoscilador.isSelected());
			ch_.rellenaTodo();*/
			}

		

	}
	private void CalculaBeneficios(){
		
		//Double benef_;
		isi_estudios estu1 =new isi_estudios(null,null,"",false);
		estu1.IniciaAcumulado(this.tPeriodo.getText());
		//estu1.IniciaOscilador(textsocparam.getText());
		estu1.IniciaMAC(textField_1.getText());
		//estu1.Inicia_signal(textField_4.getText());
		estu1.Inicia_signal(Limite_entrada.getText(),
				  Limite_salida.getText(),
		  Limite_entrada_tendencia_1.getText(),
		  Limite_entrada_tendencia_2.getText() ,
		  Limite_perdidas.getText(),
		  Limite_movil_beneficios.getText(),
		  Triger_beneficios.getText(),
		  Limite_entrada_tendencia_MED_1.getText(),
		  Limite_entrada_tendencia_MED_2.getText(),
		  MaximaDistanciaEntrada.getText()
				);
		
		//estu1.IniciaMedias(textField_medias.getText());
		estu1.IniciaMedias (texMedia_R.getText(),texMedia_I.getText(),texMedia_L.getText(),ModuloDesviacion.getText(),BaseDesviacion.getText());
		estu1.cargaSql(isi_util.CampoTexto(textField1.getText(),0,"_"));
		//benef_=estu1.CalculaBeneficios(true);
		String calcula_ = String.valueOf(estu1.CalculaBeneficios(true,false))+"-"+String.valueOf(estu1.CalculaBeneficios(true,true));
		textField_2.setText(calcula_);
	}
	private void cargaComboBeneficios(){
    	
		
		    
			
		
		/*barras_trade barras_ = new barras_trade();
		barras_.cargaSql("");
		Iterator<String> itr = barras_.fechas.iterator();
		list_1.removeAll();
		DefaultListModel model = new DefaultListModel();
			
		while (itr.hasNext()) {
			String element=itr.next();
			if(!element.isEmpty()){
			isi_estudios estu1 =new isi_estudios(null,null);
			estu1.cargaSql(element);
			estu1.IniciaOscilador(14,3,7,1);
			model.add(model.getSize(),Double.toString(estu1.CalculaBeneficios(Integer.parseInt(spinner.getValue().toString()))));
			}
			
		}
		list_1.setModel(model);*/
		
		isiBenChart beneficio_chart= new isiBenChart("Beneficios "+spinner.getValue().toString());
		ChartPanel panel2=beneficio_chart.CreaChart(Integer.parseInt(spinner.getValue().toString()),textField_1.getText(),
				estudiosProperties()
				,
				texMedia_R.getText()+","+
				texMedia_I.getText()+","+
				texMedia_L.getText()+","+
				ModuloDesviacion.getText()+","+
				BaseDesviacion.getText()
				,isi_util.isiParseInt(this.tPeriodo.getText())
				);
		Grafico frame2 = new Grafico(panel2);
		frame2.setVisible(true);
		//graficos.add(dualtimechart);
    }
	
	private void CambiaLimites(){
		CambiaLimites(charSelected(textField1.getText()));
				
	}
}
